{extend name="common/page"}
{block name="content"}
<kblock>
    <button class="layui-btn layui-btn-danger" id="del-all-btn"><i class="layui-icon"></i>批量删除</button>
    <button class="layui-btn" onclick="$k.adminShow('添加产品','/admin/product/add')"><i class="layui-icon"></i>添加</button>
    <button class="layui-btn" onclick="$k.reload(tableInst, '/admin/product/all');" id="reload">
        <i class="layui-icon">&#xe669;</i> 刷新
    </button>
    <span class="k-right" style="line-height:40px"></span>
</kblock>
<table id="privilege-category" lay-filter="privilege-category"></table>

<script type="text/html" id="operateBar">
    <a class="layui-btn layui-btn-sm" lay-event="detail">看</a>
    <a class="layui-btn layui-btn-sm" lay-event="edit">改</a>
    <a class="layui-btn layui-btn-sm" lay-event="del">删</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
    {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="check">审核</a>
    {{#  } }}
</script>
{/block}

{block name="script"}
{__block__}
<script>
    var tableInst = null;
    layui.use(['table', 'util'], function() {
        var table = layui.table;
        // render table
        var categoryData = {$products|raw};
        tableInst = table.render({
            elem: '#privilege-category',
            height: 'full-100',
            data: categoryData,
            page:true,
            limit: 10,
            cols: [[
                {checkbox: true},
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'name', title: '标题'},
                {title: '栏目', templet: '<div>{{d.category.name}}</div>', width: 200},
                {title: '显示', width: 80, templet: function (d) {
                        return d.status === 1 ? '开启' : '关闭';
                    }},
                {title: '发布时间', width: 150, templet: '<div>{{layui.util.timeAgo(d.create_time, true)}}</div>'},
                {title: '操作', fixed: 'right', width: 200, align: 'center', toolbar: '#operateBar'}

            ]]
        });

        // 删除选中
        $('#del-all-btn').on('click', function () {
            $k.delChecked('privilege-category', '/admin/product/del', tableInst, '/admin/product/all');
        })
        // tool bar
        table.on('tool(privilege-category)', function (obj) {
            if (obj.event === 'detail') {
                $k.adminShow('产品详情', '/admin/product/detail/'+obj.data.id)
            }
            if (obj.event === 'edit') {
                $k.adminShow('产品编辑', '/admin/product/edit/'+obj.data.id)
            }
            if (obj.event === 'del') {
                $k.del(obj.data.id, '/admin/product/del', tableInst, '/admin/product/all')
            }
        })


    });
</script>
{/block}